﻿using System;
using System.ServiceModel.Activation;
using WEIShareWCFRole.BusinessEntities;
using WEIShareWCFRole.BusinessEntities.Paging;
using System.ServiceModel;

namespace WEIShareWCFRole
{        
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WindowsExperienceService : IWindowsExperienceService
    {
        public DateTime GetUtcDateTime()
        {
            return DateTime.UtcNow;
        }

        public Response SubmitWindowsAssessment(BusinessEntities.WindowsAssessment windowsAssessmentEntity, string key)
        {
            Response response = new Response();

            bool keyAccepted = true;
            string decryptedKey = BaseCode.Helpers.Cryptor.Decrypt(key);
            if (!decryptedKey.Contains("|"))
            {
                keyAccepted = false;
                response.StatusMessage = "Invalid Key";
            }
            else
            {
                string password = decryptedKey.Substring(0, decryptedKey.IndexOf('|'));
                string version = decryptedKey.Substring(decryptedKey.IndexOf('|') + 1);

                if(!version.Equals("1.0", StringComparison.OrdinalIgnoreCase))
                {
                    keyAccepted = false;
                    response.StatusMessage = "Incorrect version.  Please download the latest version of the application from the web site.";
                }
                else if (!password.Equals("EE05BACB-CEA7-422F-B64F-FF8B402743EB", StringComparison.OrdinalIgnoreCase))
                {
                    keyAccepted = false;
                    response.StatusMessage = "Invalid Key Code";
                }
            }

            if (keyAccepted)
            {
                if (windowsAssessmentEntity.SystemScore.HasValue)
                {
                    if (!BusinessLogicLayer.WindowsAssessment.WindowsAssessementAlreadyRecorded(windowsAssessmentEntity))
                    {
                        BusinessLogicLayer.WindowsAssessment.Save(windowsAssessmentEntity);
                    }
                    else
                    {
                        //Not error out...it compares mac address and the scores to see if it already exists
                        //response.StatusCode = 500;
                        response.StatusMessage = "Windows Experience Index has already been recorded for this user";
                    }
                }
                else
                {
                    response.StatusCode = 500;
                    response.StatusMessage = "Invalid System Score";
                }
            }
            else
            {
                response.StatusCode = 200;
            }


            return response;
        }

        public Response<PageResponse<WindowsAssessment>> GetPagedWindowsAssessment(PageRequest pageRequest)
        {
            Response<PageResponse<WindowsAssessment>> response = new Response<PageResponse<WindowsAssessment>>();

            response.Data = BusinessLogicLayer.WindowsAssessment.GetPaged(pageRequest);

            return response;
        }


        public Response<PageResponse<WindowsAssessment>> GetPagedAdvancedSearchWindowsAssessment(PageRequest pageRequest, AdvancedSearch advancedSearch)
        {
            Response<PageResponse<WindowsAssessment>> response = new Response<PageResponse<WindowsAssessment>>();

            response.Data = BusinessLogicLayer.WindowsAssessment.GetPaged(pageRequest, advancedSearch);

            return response;
        }
    }
}

